package com.caiyigo.baihua.utils;

import java.lang.reflect.Field;
import java.util.List;

public class ListUtil {

	public static int sum(List<Integer> list) {

		int max = 0;

		for (Integer i : list) {

			max += i;
		}
		return max;
	}

	public static Object[][] stringToArray(Object obj, String objArray) {
		String[] array = objArray.split(",");
		Object[][] objs = new Object[array.length][2];

		for (int i = 0; i < array.length; i++) {

			objs[i] = new Object[] { obj, Integer.parseInt(array[i]) };
		}

		return objs;
	}

	public static Object[][] ArrayToArray(Integer obj, Integer[] objArray) {
		Object[][] objs = new Object[objArray.length][2];

		for (int i = 0; i < objArray.length; i++) {
			objs[i] = new Object[] { obj, objArray[i] };
		}

		return objs;

	}

	public static Object[][] ArrayToArray(String ids, Integer[] objArray) {
		String[] idArray = ids.split(",");

		Object[][] objs = new Object[objArray.length * idArray.length][2];

		int z = 0;
		for (int j = 0; j < idArray.length; j++) {
			for (int i = 0; i < objArray.length; i++) {
				objs[z++] = new Object[] { idArray[j], objArray[i] };
			}
		}

		return objs;
	}

	public static Integer[] getParaValuesToInt(String[] values) {
		if (values == null)
			return null;
		Integer[] result = new Integer[values.length];
		for (int i = 0; i < result.length; i++)
			result[i] = Integer.parseInt(values[i]);
		return result;
	}

	public static String listToString(List list, String fileName) {
		String str = "";
		for (int i = 0; i < list.size(); i++) {
			// 获取对象中名为"passWord"元素
			Field fields;
			try {
				fields = list.get(i).getClass().getDeclaredField(fileName);
				if (!fields.isAccessible())// 判断该对象是否可以访问
				{
					fields.setAccessible(true);// 设置为可访问
				}

				str += fields.get(list.get(i)) + ",";
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (str.length() > 1)
			return str.substring(0, str.length() - 1);
		else
			return str;
	}
}
